/*
 * Copyright (c) 2023, 2023,yzc.cn All rights reserved.
 *
 */
package com.crmapi.service.impl;

import com.crmapi.mapper.DeptMapper;
import com.crmapi.mapper.UserMapper;
import com.crmapi.pojo.PageBean;
import com.crmapi.pojo.User;
import com.crmapi.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * <p>Project: CRMSystem - UserServiceImpl</p>
 * <p>Powered by yzc On 2023-11-15 16:37:03</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;


    //查询用户
    @Override
    public PageBean queryUser(Integer page,Integer pageSize,String name,String deptName,String jobName, Integer status) {

         //1.设置分页参数
        PageHelper.startPage(page,pageSize);
         //2.执行查询
         List<User> userList=userMapper.queryUser(name, deptName, jobName, status);
         //3.格式化
        PageInfo pageInfo = new PageInfo<>(userList);
         //4.封装pageBean中
        PageBean pageBean = new PageBean(pageInfo.getTotal(),userList);
        return pageBean;
    }

    //保存用户
    @Override
    public int save(User entity) {
        //生成用户编码
        //根据部门编码  查询最大的员工编码

        String maxCode = userMapper.queryMaxNo(entity.getDeptNo());
        String newCode=null;
        if (maxCode==null){
            //该部门没有客户
            newCode=entity.getDeptNo()+"001";
        }else {
            newCode=String.valueOf (Integer.parseInt(maxCode)+1);
        }
        entity.setCode(newCode);
        return userMapper.save(entity);
    }

    //批量删除
    @Override
    public int delByIds(List<String> ids) {
        return userMapper.delByIds(ids);
    }

    //更新用户
    @Override
    public int update(User user) {
     String maxCode = userMapper.queryMaxNo(user.getDeptNo());
     String newCode = String.valueOf(Integer.parseInt(maxCode)+1);
     user.setCode(newCode);
     return userMapper.update(user);
    }

    @Override
    public User userLogin(String username) {
        return userMapper.userLogin(username);
    }

    //用户登录


    //修改密码
    @Override
    public int updatePassword(String id, String password) {
            return userMapper.updatePassword(id,password);
    }


    //查找相同用户名
    @Override
    public int queryUsername(String username) {
        return userMapper.queryUsername(username);
    }
}
